package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: wenxiwen
 * Date: 2022/4/13
 * Time: 17:28
 * Description: No Description
 */
public class Del implements IOperation{

    public void work(BookList booklist) {
        System.out.println("删除图书");

        int count = 0;
        System.out.println("请输入你要删除的图书名称：");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine();
        int index = booklist.getCount();
        int i = 0;
        for (i = 0; i < index; i++) {
            Book book = booklist.getBook(i);
            if(name.equals(book.getName())) {
                count = i;
                break;
            }
        }
        if(i == index) {
            System.out.println("没有找到你要删除的图书！");
            return;
        }

        for (int j = count; j < index - 1; j++) {
            Book book = booklist.getBook(j+1);
            booklist.setBook(j,book);
        }

        booklist.setBook(index-1,null);
        booklist.setCount(index-1);
        System.out.println("删除成功！");
    }
}